package com.bootdo.hjh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bootdo.hjh.dao.UserAddressMapper;
import com.bootdo.hjh.domain.UserAddress;
import com.bootdo.hjh.service.UserAddressService;


@Service
public class UserAddressServiceImpl implements UserAddressService{
	
	@Autowired UserAddressMapper userAddressMapper;
	
	@Override
	@Transactional
	public List<UserAddress> getUserAddressAll(Long userId) {
		return userAddressMapper.getUserAddressForAll(userId);
	}

	@Override
	public UserAddress getUserAddressById(Integer id) {
		return userAddressMapper.getUserAddressById(id);
	}

	@Override
	public UserAddress getUserAddressByDefault(Long userId) {
		UserAddress ua = userAddressMapper.getUserAddressDefault(userId);
		if(ua == null){
			ua = userAddressMapper.getUserAddressMaxId(userId);
		}
		return ua;
	}
	
	@Override
	public UserAddress addUserAddress(UserAddress userAddress) {
		//verifyAddress(userAddress);
		if(userAddress.getIsDefault() == 1){
			userAddressMapper.updateUserAddressDefaultToNone(userAddress.getUserId());
		}
		userAddressMapper.addUserAddress(userAddress);
		return userAddress;
	}

	@Override
	public UserAddress modifyUserAddress(UserAddress userAddress) {
		//verifyAddress(userAddress);
		if(userAddress.getId() == null || userAddress.getId() <= 0){
			throw new RuntimeException("ID不能为空");
		}
		if(userAddress.getIsDefault() == 1){
			userAddressMapper.updateUserAddressDefaultToNone(userAddress.getUserId());
		}
		userAddressMapper.modifyUserAddress(userAddress);
		return userAddress;
	}
	
	private void verifyAddress(UserAddress userAddress){
		
		if(userAddress.getProvinceId() == null){
			throw new RuntimeException( "省ID不能为空");
		}
		if(userAddress.getProvince() == null || userAddress.getProvince().isEmpty()){
			throw new RuntimeException( "省名称不能为空");
		}
		if(userAddress.getCityId() == null){
			throw new RuntimeException( "城市ID不能为空");
		}
		if(userAddress.getCity() == null || userAddress.getCity().isEmpty()){
			throw new RuntimeException("城市名称不能为空");
		}
		if(userAddress.getDistrictId() == null){
			throw new RuntimeException( "地区ID不能为空");
		}
		if(userAddress.getDistrict() == null || userAddress.getDistrict().isEmpty()){
			throw new RuntimeException("地区名称不能为空");
		}
		if(userAddress.getAddress() == null || userAddress.getAddress().isEmpty()){
			throw new RuntimeException( "详细地址不能为空");
		}
		if(userAddress.getName() == null || userAddress.getName().isEmpty()){
			throw new RuntimeException( "收件人不能为空");
		}
		if(userAddress.getMobile() == null || userAddress.getMobile().isEmpty()){
			throw new RuntimeException("手机号码不能为空");
		}
	}

	@Override
	public void updateAddressToDefault(UserAddress userAddress) {
		userAddressMapper.updateUserAddressDefaultToNone(userAddress.getUserId());
		userAddressMapper.updateUserAddressDefault(userAddress);
	}

	@Override
	public void deleteUserAddress(Integer id) {
		userAddressMapper.deleteUserAddress(id);
	}

	@Override
	public int saveOrUpdateUserAddress(UserAddress address) {
		if(address.getId()!=null && address.getId()>0){
			//修改地址
			UserAddress userAddressOra = userAddressMapper.getUserAddressById(address.getId());
			if(userAddressOra!=null){
				userAddressOra.setAddress(address.getAddress());
				userAddressOra.setName(address.getName());
				userAddressOra.setMobile(address.getMobile());
				this.modifyUserAddress(userAddressOra);
			}
		}else{
			addUserAddress(address);
		}

		return 0;
	}
}
